package com.ou.csbs.capstone.models.shapes;

import gov.nasa.worldwind.geom.Position;
import gov.nasa.worldwind.render.BasicShapeAttributes;
import gov.nasa.worldwind.render.Renderable;
import gov.nasa.worldwind.render.SurfacePolyline;
import gov.nasa.worldwind.util.measure.LengthMeasurer;

import java.util.ArrayList;
import java.util.Iterator;

import com.ou.csbs.capstone.models.AttributeModel;
import com.ou.csbs.capstone.models.RenderableModel;
import com.ou.csbs.capstone.views.worldwind.WorldWindCanvas;
/**
 * A drawn line object. This could be a single line, or a multi-segmented line.
 * It is extended off of a SurfacePolyline so that any picking that occurs during the EditingTool process does not interfere with existing SurfacePolyline.
 * Secondly, the constructors and certain methods have been added to aid in object creation, in adherence to how the drawing tools are designed to work.
 * 
 * @author Senior Capstone
 */
public class Line extends SurfacePolyline{
	private ArrayList<Position> positions = new ArrayList<Position>();
	
	public Line(Position begin, Position end){
		this.setAttributes(AttributeModel.getAttributes());
		this.positions.add(begin);
		this.positions.add(end);
		this.setLocations(this.positions);
	}
	public Line(BasicShapeAttributes bsa, Position begin, Position end){
		this.setAttributes(bsa);
		this.positions.add(begin);
		this.positions.add(end);
		this.setLocations(this.positions);
	}
	public Line(ArrayList<Position> positions){
		this.setAttributes(AttributeModel.getAttributes());
		this.positions = new ArrayList<Position>(positions);
		this.setLocations(this.positions);
	}
	public Line(BasicShapeAttributes bsa, ArrayList<Position> positions){
		this.setAttributes(bsa);
		this.positions = new ArrayList<Position>(positions);
		this.setLocations(this.positions);
	}
	
	public void drawShape(RenderableModel model) {
		model.getDrawingLayer().addRenderable(this);
		WorldWindCanvas.getWorldWindow().redrawNow();
	}

	public void removeShape(RenderableModel model) {
		model.getDrawingLayer().removeRenderable(this);	
		WorldWindCanvas.getWorldWindow().redrawNow();
	}
	
	public boolean isDrawn(RenderableModel model) {
		Iterator<Renderable> it =  model.getDrawingLayer().getRenderables().iterator();
		while(it.hasNext()){
			if(it.next().equals(this))
				return true;
		}
		return false;
	}
	
	public void updatePoint(int index, Position newPosition){
		this.positions.set(index, newPosition);
		this.setLocations(this.positions);
		WorldWindCanvas.getWorldWindow().redrawNow();
	}
	public void addPosition(Position newPosition){
		this.positions.add(newPosition);
		this.setLocations(this.positions);
		WorldWindCanvas.getWorldWindow().redrawNow();
	}
	public void removePosition(int index){
		this.positions.remove(index);
		this.setLocations(this.positions);
		WorldWindCanvas.getWorldWindow().redrawNow();
	}
	public double getLength(){
		LengthMeasurer lm = new LengthMeasurer(this.positions);
		return lm.getLength(WorldWindCanvas.getWorldWindModel().getGlobe());
	}
}
